package com.etown.util.email;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;

import org.apache.log4j.Logger;
/**
 * 电子邮件发送器
 * @author knight
 *
 */
public class EMailSender {
	private String smtpHost = "smtp.126.com";

	private int port = 25;

	private String userName;

	private String passWord;

	private String protocol = "smtp";

	private String mailFrom;

	private static Logger log=Logger.getLogger(EMailSender.class);
	
	public EMailSender() {
	}

	public EMailSender(String smtpHost, String userName, String passWord) {
		this.smtpHost = smtpHost;
		this.userName = userName;
		this.passWord = passWord;
	}

	public EMailSender(String smtpHost, int port, String userName, String passWord) {
		this.smtpHost = smtpHost;
		this.userName = userName;
		this.port = port;
		this.passWord = passWord;
	}

	/**
	 * 单发
	 * @param mailMessage
	 * @return
	 * @throws NoSuchProviderException
	 * @throws MessagingException
	 */
	public boolean send(MailMessage mailMessage){
		return send(new MailMessage[] { mailMessage });
	}
	
	/**
	 * 群发邮件
	 * @param mailFrom
	 * @param subject
	 * @param content
	 * @param cc
	 * @param receivers
	 * @return
	 */
	public boolean groupSend(String mailFrom,String subject,String content,String[] receivers,String[] cc){
		if(receivers==null||receivers.length==0){
			return false;
		}
		try{
			ArrayList<MailMessage> mails=new ArrayList<MailMessage>();
			for(int i=0;i<receivers.length;i++){
				MailMessage m=new MailMessage();
				m.setMailFrom(mailFrom);
				m.setMailTo(receivers[i]);
				if(null!=cc&&cc.length!=0){
					m.setMailCc(cc);
				}
				m.setMaiSubject(subject);
				m.setContent(content);
				m.saveChanges();
				mails.add(m);
			}
			MailMessage[] mArr=(MailMessage[]) mails.toArray(new MailMessage[0]);
			return this.send(mArr);
		}catch(Exception e){
			log.error("群发邮件失败"+e.getMessage());
		}
		return false;
	}
	
	/**
	 * 一次发送多份
	 * @param mailMessage
	 * @return
	 * @throws NoSuchProviderException
	 * @throws MessagingException
	 */
	public boolean send(MailMessage[] mailMessage){
		Transport transport = null;
		boolean sendSuccess = true;
		try {
			Properties props = new Properties();
			props.put("mail." + this.protocol + ".auth", "true");
			props.put("mail.transport.protocol", this.protocol);
			props.put("mail." + this.protocol + ".host", this.smtpHost);
			props.put("mail." + this.protocol + ".port", String.valueOf(this.port));
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport(this.protocol);
			transport.connect(this.smtpHost, this.userName, this.passWord);
			
			for (int i = 0; i < mailMessage.length; ++i) {
				mailMessage[i].setSession(session);
				transport.sendMessage(mailMessage[i].getMessage(),mailMessage[i].getMessage().getAllRecipients());
			}
		} catch (Exception e) {
			log.error("发送多份邮件失败"+e.getMessage());
			e.printStackTrace();
			sendSuccess = false;
		} finally {
			if (transport != null){
				try {
					transport.close();
				} catch (MessagingException e) {
					log.error("发送多份邮件出现异常"+e.getMessage());
				}
			}
		}
		return sendSuccess;
	}

	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getMailFrom() {
		return this.mailFrom;
	}

	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}
	
	public static void main(String[] args) throws MessagingException{
		EMailSender sender = new EMailSender();
		sender.setPort(25);
		sender.setUserName("damoqiongqiu");
		sender.setPassWord("xiaotao015001024");
		sender.setMailFrom("damoqiongqiu@126.com");
		
		//单发
//		MailMessage mail1 = new MailMessage();
//		mail1.setMailFrom(sender.getMailFrom());
//		mail1.setMailTo("253445528@qq.com");
//		mailMessage.setMailCc((String[])(mailCc.toArray(new String[0])));
//		mail1.setMaiSubject("测试邮件");
//		mail1.setContent("这是一份测试邮件啊！！！从JavaMail自动发出啊！！！不要回复啊！！！");
//		mail1.saveChanges();
//		sender.send(mail1);
		
		//群发
		sender.groupSend(sender.getMailFrom(), "测试邮件", "超长的内容啊 ！！！",new String[]{"253445528@qq.com","58482296@qq.com"}, null);
	}
}